package com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageAllocatingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageResultActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou.CYShoppingInStorageListAdapter;
import com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou.CYShoppingOverViewAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingOverviewBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageFreeConfirmBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageFreeConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageShoppingConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageShoppingOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageAllocatingOverviewRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageFreeConfirmRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYInStorageShoppingConfirmRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYInStorageShoppingOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYInStorageUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/27 20:03
 * @email siyu.wang@inossem.com
 */
public class CYInStorageShoppingOverViewActivity extends BaseActivity {
    private TextView title , nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private CYShoppingOverViewAdapter adapter;

    private List<CYInStorageShoppingOverviewBean.ItemBean.ItemListBean> allData;

    private String stockInputId , orderNumber;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_shopping_list);
        SPUtils.removeInStorageShoppingOverViewList(CYInStorageShoppingOverViewActivity.this);
        initView(CYInStorageShoppingOverViewActivity.this);
        initData(CYInStorageShoppingOverViewActivity.this);

    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        mCondition = "";
        getData(CYInStorageShoppingOverViewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }
    private void initData(Activity activity) {
        title.setText(R.string.cy_activity_in_storage_shopping_overview_title);
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(CYInStorageShoppingOverViewActivity.this, getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();

            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirm(activity);
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            switch (QRCodeUtils.getQRCodeType(scanResult)) {
                case QRCodeConstant.CY_TYPE_BATCH:
                    mCondition = QRCodeUtils.DecryptQRCode(scanResult , QRCodeConstant.CY_BATCH_PARAMETER_BATCH);
                    String itemId = CYInStorageUtils.getItemIdByBatch(activity , mCondition);
                    if (TextUtils.isEmpty(itemId)) {
                        Utils.middleToast(activity , "没有找到批次");
                    }else {
                        LogUtils.i("itemId ==== " + itemId);
                        Intent intent = new Intent(activity, CYInStorageShoppingDetailActivity.class);
                        intent.putExtra(InStorageExtra.ITEM_ID, itemId);
                        startActivity(intent);
                    }
                    break;
                    default:
                        Utils.middleToast(activity , "扫描码类型错误");
                        break;
            }

        }, QRCodeConstant.CY_TYPE_BATCH);
    }
    private void getExtra(Activity activity) {
        stockInputId = activity.getIntent().getStringExtra(InStorageExtra.STOCK_INPUT_ID);
        orderNumber = activity.getIntent().getStringExtra(InStorageExtra.ORDER_NUMBER);
    }
    private void addInStorageShoppingItemListData (Context context, List<CYInStorageShoppingOverviewBean.ItemBean.ItemListBean> list ,CYInStorageShoppingOverviewBean.ItemBean bean , ListView listView, boolean flag) {
        if (flag) {
            adapter = new CYShoppingOverViewAdapter(context , mCondition , orderNumber , list , bean , new InStorageShoppingOverviewListener() {
                @Override
                public void onNoDoubleClickListener(String batch) {

                    String itemId = CYInStorageUtils.getItemIdByBatch(context , batch);
                    if (TextUtils.isEmpty(itemId)) {
                        Utils.middleToast(context , "没有找到批次");
                    }else {
                        LogUtils.i("itemId ==== " + itemId);
                        Intent intent = new Intent(context, CYInStorageShoppingDetailActivity.class);
                        intent.putExtra(InStorageExtra.ITEM_ID, itemId);
                        startActivity(intent);
                    }

                }
            });
            listView.setAdapter(adapter);
        }
    }
    private void getData(Context context) {
        String allocatingInStorageInformation = SPUtils.getInStorageShoppingOverViewlList(context);
        if (TextUtils.isEmpty(allocatingInStorageInformation)) {
            CYInStorageShoppingOverviewRequest.request(context, stockInputId, new GsonBeanListener<CYInStorageShoppingOverviewBean>() {
                @Override
                public void onSuccess(CYInStorageShoppingOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, CYInStorageShoppingOverviewBean.class);
                    SPUtils.saveInStorageShoppingOverViewlList(context, json);
                    CYInStorageShoppingOverviewBean.ItemBean data = bean.getBody();
                    allData = bean.getBody().getItem_list();
                    addInStorageShoppingItemListData(context,  ActivityUtils.getShowData(allData, 0), data,materielList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            CYInStorageShoppingOverviewBean bean = gson.fromJson(allocatingInStorageInformation, CYInStorageShoppingOverviewBean.class);
            if (TextUtils.isEmpty(mCondition)) {
                allData = bean.getBody().getItem_list();
            } else {
                allData = CYInStorageUtils.getShoppingInStorageList(context, mCondition);
            }
            addInStorageShoppingItemListData(context, ActivityUtils.getShowData(allData, 0),bean.getBody(),  materielList, true);
        }
    }

    private void confirm(Context context) {
        CYInStorageShoppingConfirmRequestBean params = CYInStorageUtils.getInStorageShoppingConfirmRequestBean(context);
//        Gson gson = new Gson();
//        LogUtils.i(" params ==== " + gson.toJson(params));
        CYInStorageShoppingConfirmRequest.request(context, params, new GsonBeanListener<CYInStorageShoppingConfirmBean>() {
            @Override
            public void onSuccess(CYInStorageShoppingConfirmBean bean) {
//                if (SUCCESS.equals(bean.getBody().getCode())) {
                SPUtils.removeInStorageShoppingOverViewList(CYInStorageShoppingOverViewActivity.this);
                Intent intent = new Intent(context, InStorageResultActivity.class);
                intent.putExtra(InStorageExtra.MESSAGE, bean.getHead().getMsg());
                intent.putExtra(InStorageExtra.CONFIRM_MESSAGE, context.getResources().getString(R.string.activity_in_storage_receive_confirm_success));
                startActivity(intent);
            }

            @Override
            public void onFailing(CYInStorageShoppingConfirmBean bean) {
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
            }
        });
    }

    public interface InStorageShoppingOverviewListener {
        void onNoDoubleClickListener(String batch);
    }
    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(CYInStorageShoppingOverViewActivity.this, getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                CYInStorageShoppingOverViewActivity.super.onBackPressed();
            }
        }).show();
    }
}
